package main;


import connectivity.Dbmanager;
import connectivity.QueryManager;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.DecimalFormat;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import model.Klant;

/**
 *
 * @author Administrator
 */
public final class WinkelApplication 
{
    private static final Dimension SCREEN_DIMENSION = Toolkit.getDefaultToolkit().getScreenSize();
    
    /** Define frame width, height and name*/
    public static final int FRAME_WIDTH = SCREEN_DIMENSION.width;
    public static final int FRAME_HEIGHT = SCREEN_DIMENSION.height;
    public static final String NAME = "WinkelApplicatie";
    public static final String CURRENCY = "\u20AC";
    //** static decimal format which is used to format prices */
    public static final DecimalFormat PRICE_FORMAT = new DecimalFormat("###,###,##0.00");
    /** static fonts which are used within the application */
    public static final Font FONT_10_PLAIN = new Font("Verdana", Font.PLAIN, 10);
    public static final Font FONT_10_BOLD = new Font("Verdana", Font.BOLD, 10);
    public static final Font FONT_12_BOLD = new Font("Verdana", Font.BOLD, 12);
    public static final Font FONT_14_PLAIN = new Font("Verdana", Font.PLAIN, 14);
    public static final Font FONT_16_PLAIN = new Font("Verdana", Font.PLAIN, 16);
    public static final Font FONT_16_BOLD = new Font("Verdana", Font.BOLD, 16);
    /** database manager */
    private Dbmanager dbManager;
    private QueryManager queryManager;
    /** models used in the application */
    private model.Basket basket;
    /** Klant */
    private model.Klant klant;
    /** the main window */
    private JFrame mainWindow;
    /** singleton of the application */
    private static WinkelApplication instance = new WinkelApplication();
    
    private int accessLevel;
    
    private int lastCategoryId;
    
    /**
     * You shouldn't be creating an instance of a singleton. 
     * Use <code>WinkelApplication.getInstance()</code> instead.
     */
    private WinkelApplication() { }
    
    public int getLastCategory()
    {
        return lastCategoryId;
    }
    
    public void setLastCategory(int lastCategoryId)
    {
        this.lastCategoryId = lastCategoryId;
    }
    
    public int getAccesLevel()
    {
        return accessLevel;
    }    
    
    public void setAccesLevel(int acessLevel)
    {
        this.accessLevel = acessLevel;
    }
    
    /**
     * Computes a font size depending on the parameter <code>size</code> and the
     * size of the screen.
     * @param size Must be either 1, 2 or 3. When this parameter is 1 this method
     * will return a large font, 2 will return a medium sized font and 3 will return
     * a small font.
     * @return A font based on the screen size and the parameter <code>size</code>
     */
    public static Font computeFontSize(int size)
    {
        Font font = new Font("Verdana", Font.BOLD, 16);
        if (SCREEN_DIMENSION.height<=600){
            switch(size){
                
                case 1:
                    font = new Font("Verdana", Font.BOLD, 16);
                    break;
                case 2:
                    font = new Font("Verdana", Font.BOLD, 12);
                    break;
                case 3:
                    font = new Font("Verdana", Font.PLAIN, 10);
                    break;
            }
        }
        
        else if (SCREEN_DIMENSION.height<=750 && SCREEN_DIMENSION.height>600){
            switch(size){
                
                case 1:
                    font = new Font("Verdana", Font.BOLD, 20);
                    break;
                case 2:
                    font = new Font("Verdana", Font.BOLD, 16);
                    break;
                case 3:
                    font = new Font("Verdana", Font.PLAIN, 14);
                    break;
            }
        }
        
        else if (SCREEN_DIMENSION.height<=900 && SCREEN_DIMENSION.height>750){
            switch(size){
                
                case 1:
                    font = new Font("Verdana", Font.BOLD, 25);
                    break;
                case 2:
                    font = new Font("Verdana", Font.BOLD, 20);
                    break;
                case 3:
                    font = new Font("Verdana", Font.PLAIN, 16);
                    break;
            }
        }
        
        else if (SCREEN_DIMENSION.height<=1080 && SCREEN_DIMENSION.height>900){
            switch(size){
                
                case 1:
                    font = new Font("Verdana", Font.BOLD, 30);
                    break;
                case 2:
                    font = new Font("Verdana", Font.BOLD, 22);
                    break;
                case 3:
                    font = new Font("Verdana", Font.PLAIN, 18);
                    break;
            }
        }
        return font;        
    }
           
    public void initialize() {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e) {
            System.err.println("Error setting LookAndFeelClassName: " + e);
        }
        // create and initialize the connectivity
        dbManager = new Dbmanager();
        dbManager.openConnection();
        queryManager = new QueryManager(dbManager);

        // create an empty basket
        basket = new model.Basket();
    }

    public void startup() {
        klant = queryManager.getKlant(1);
        
        mainWindow = new JFrame(NAME);
        mainWindow.setSize(FRAME_WIDTH, FRAME_HEIGHT);
        mainWindow.setUndecorated(true);
        /** Make the window closing [x] button on the frame active */
        mainWindow.addWindowListener(new WindowAdapter() {

            @Override
            public void windowClosing(WindowEvent event) {
                shutdown();
            }
        });

        mainWindow.getContentPane().setLayout(new BorderLayout());
        showPanel(new view.LogInScreen());

        mainWindow.setVisible(true);
    }

    public void showPanel(JPanel panel) {
        mainWindow.getContentPane().removeAll();
        mainWindow.getContentPane().add(panel, BorderLayout.CENTER);
        mainWindow.getContentPane().validate();
        mainWindow.getContentPane().repaint();
    }

    public void exit() {
        mainWindow.setVisible(false);
        shutdown();
    }

    private void shutdown() {
        mainWindow.dispose();
        dbManager.closeConnection();
    }

    /**
     * @return the instance of this class
     */
    public static WinkelApplication getInstance() {
        return instance;
    }

    /**
     * @return the queryManager
     */
    public static QueryManager getQueryManager() {
        return getInstance().queryManager;
    }

    /**
     * @return the basket
     */
    public static model.Basket getBasket() {
        return getInstance().basket;
    }
    
    public static model.Klant getKlant() {
        return getInstance().klant;
    }

    public static void main(String args[]) {
        final WinkelApplication applicatie = WinkelApplication.getInstance();
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                try {
                    applicatie.initialize();
                    applicatie.startup();
                } catch (Exception e) {
                    System.out.println("Application " + applicatie.getClass().getName() + " failed to launch");
                }
            }
        });
    }
}
